/*
 * Copyright (C) 2010 Rodion Volkov (volkov.rodion@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.rchik.divermate;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.admob.android.ads.AdManager;

public class DiveTableSelection extends ListActivity {
	
	private DiveTablesDBAdapter mDbHelper;
    private Cursor mDivingTablesCursor;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AdManager.setTestDevices( new String[] { "2D8CEAD2C421ECA54C164FB295BCA502" } );
        
        mDbHelper = new DiveTablesDBAdapter(this);
        mDbHelper.open();
        mDivingTablesCursor = mDbHelper.fetchAllTables();
        
        if ( mDivingTablesCursor.getCount() > 1)
        {
	        setContentView(R.layout.main);
	   
	        fillData();
        }
        else
        {
        	mDivingTablesCursor.moveToFirst();
        	Intent i = new Intent(this, DivingMenu.class);
        	i.putExtra(DiveTablesDBAdapter.KEY_DIVINGTABLES_ROWID, 0);
        	i.putExtra(DiveTablesDBAdapter.KEY_DIVINGTABLES_TITLE, mDivingTablesCursor.getString(
        	        mDivingTablesCursor.getColumnIndexOrThrow(DiveTablesDBAdapter.KEY_DIVINGTABLES_TITLE)));
        	startActivity(i);
        	
        	finish();
        }
    }
    
    private void fillData() {
        // Get all of the rows from the database and create the item list
        
        startManagingCursor(mDivingTablesCursor);
        
        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{DiveTablesDBAdapter.KEY_DIVINGTABLES_TITLE};
        
        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.TableNameImageView};
        
        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter notes = 
        	    new SimpleCursorAdapter(this, R.layout.diving_table_row, mDivingTablesCursor, from, to);
        notes.setViewBinder(new DiveTableRowBinder());
        setListAdapter(notes);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	super.onListItemClick(l, v, position, id);
    	
    	Cursor c = mDivingTablesCursor;
    	c.moveToPosition(position);
    	Intent i = new Intent(this, DivingMenu.class);
    	i.putExtra(DiveTablesDBAdapter.KEY_DIVINGTABLES_ROWID, position);
    	i.putExtra(DiveTablesDBAdapter.KEY_DIVINGTABLES_TITLE, c.getString(
    	        c.getColumnIndexOrThrow(DiveTablesDBAdapter.KEY_DIVINGTABLES_TITLE)));
    	startActivity(i);
    }
}